﻿using gcloud_voice;
using UnityEngine;

namespace GVoiceSdk.GCloudVoiceAgent
{
    // 实时语音模块
    public static class RealTimeVoiceAgent
    {
        public delegate void OnJoinRoomComplete(bool succ, string roomName, int memberID);

        public delegate void OnMemberVoice(int[] members, int count);

        public delegate void OnQuitRoomComplete(bool succ, string roomName, int memberID);


        public enum VoiceRole
        {
            Anchor = 1, // 主播
            Audience = 2 // 听众
        }

        private static IGCloudVoice m_voiceengine = null;

        // 加入房间（小队、主播）完成回调
        public static OnJoinRoomComplete onJoinRoomComplete;

        // 退出房间（小队、主播）完成回调
        public static OnQuitRoomComplete onQuitRoomComplete;

        // 成员状态变化通知回调
        public static OnMemberVoice onMemberVoice;

        // 初始化
        public static void Init(IGCloudVoice voiceEngine)
        {
            m_voiceengine = voiceEngine;

            SubcribeCompleteEvents();
        }

        // 注册事件完成回调
        private static void SubcribeCompleteEvents()
        {
            m_voiceengine.OnJoinRoomComplete += (GCloudVoiceCompleteCode code, string roomName, int memberID) =>
            {
                Debug.Log("IGCloudVoice.OnJoinRoomComplete c# callback , code := " + code.ToString() +
                          " , roomName := " + roomName + " , memberID := " + memberID.ToString());
                if (onJoinRoomComplete != null)
                {
                    bool succ = (code == GCloudVoiceCompleteCode.GV_ON_JOINROOM_SUCC);
                    onJoinRoomComplete(succ, roomName, memberID);
                }
            };

            m_voiceengine.OnQuitRoomComplete += (GCloudVoiceCompleteCode code, string roomName, int memberID) =>
            {
                Debug.Log("IGCloudVoice.OnQuitRoomComplete c# callback , code := " + code.ToString() +
                          " , roomName := " + roomName + " , memberID := " + memberID.ToString());
                if (onQuitRoomComplete != null)
                {
                    bool succ = (code == GCloudVoiceCompleteCode.GV_ON_QUITROOM_SUCC);
                    onQuitRoomComplete(succ, roomName, memberID);
                }
            };

            m_voiceengine.OnMemberVoice += (int[] members, int count) =>
            {
                Debug.Log("IGCloudVoice.OnMemberVoice count:" + count);
                if (onMemberVoice != null)
                {
                    onMemberVoice(members, count);
                }
            };
        }

        /// <summary>
        ///     加入房间--小队语音多对多模式
        /// </summary>
        /// <param name="roomName">房间名称</param>
        /// <returns>执行成功返回true, 执行失败返回false</returns>
        public static bool JoinTeamRoom(string roomName)
        {
            if (m_voiceengine == null)
            {
                Debug.Log("RealTimeVoice.m_voiceengine is null");
                return false;
            }

            int ret = m_voiceengine.JoinTeamRoom(roomName, 15000);
            Debug.Log("GCloudVoiceSDK JoinTeamRoom , roomName := " + roomName + " , ret := " + ret.ToString());
            if (ret != (int)GCloudVoiceErr.GCLOUD_VOICE_SUCC)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        ///     加入房间--国战语音一对多模式
        /// </summary>
        /// <param name="roomName">房间名称</param>
        /// <param name="role">用户角色</param>
        /// <returns>执行成功返回true, 执行失败返回false</returns>
        public static bool JoinNationalRoom(string roomName, VoiceRole role)
        {
            if (m_voiceengine == null)
            {
                Debug.Log("RealTimeVoice.m_voiceengine is null");
                return false;
            }

            GCloudVoiceRole _role = role == VoiceRole.Anchor ? GCloudVoiceRole.ANCHOR : GCloudVoiceRole.AUDIENCE;
            int ret = m_voiceengine.JoinNationalRoom(roomName, _role, 15000);
            Debug.Log("GCloudVoiceSDK JoinNationalRoom , roomName := " + roomName + " , _role := " + _role.ToString() +
                      " , ret := " + ret.ToString());
            if (ret != (int)GCloudVoiceErr.GCLOUD_VOICE_SUCC)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        ///     退出房间
        /// </summary>
        /// <param name="roomName">房间名称</param>
        /// <returns>执行成功返回true, 执行失败返回false</returns>
        public static bool QuitRoom(string roomName)
        {
            if (m_voiceengine == null)
            {
                Debug.Log("RealTimeVoice.m_voiceengine is null");
                return false;
            }

            int ret = m_voiceengine.QuitRoom(roomName, 15000);
            Debug.Log("GCloudVoiceSDK QuitRoom , roomName := " + roomName + " , ret := " + ret.ToString());
            if (ret != (int)GCloudVoiceErr.GCLOUD_VOICE_SUCC)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        ///     打开麦克风
        /// </summary>
        /// <returns>执行成功返回true, 执行失败返回false</returns>
        public static bool OpenMic()
        {
            if (m_voiceengine == null)
            {
                Debug.Log("RealTimeVoice.m_voiceengine is null");
                return false;
            }

            int ret = m_voiceengine.OpenMic();
            Debug.Log("GCloudVoiceSDK OpenMic , ret := " + ret.ToString());
            if (ret != (int)GCloudVoiceErr.GCLOUD_VOICE_SUCC)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        ///     关闭麦克风
        /// </summary>
        /// <returns>执行成功返回true, 执行失败返回false</returns>
        public static bool CloseMic()
        {
            if (m_voiceengine == null)
            {
                Debug.Log("RealTimeVoice.m_voiceengine is null");
                return false;
            }

            int ret = m_voiceengine.CloseMic();
            Debug.Log("GCloudVoiceSDK CloseMic , ret := " + ret.ToString());
            if (ret != (int)GCloudVoiceErr.GCLOUD_VOICE_SUCC)
            {
                return false;
            }

            return true;
        }


        /// <summary>
        ///     打开扬声器
        /// </summary>
        /// <returns>执行成功返回true, 执行失败返回false</returns>
        public static bool OpenSpeaker()
        {
            if (m_voiceengine == null)
            {
                Debug.Log("RealTimeVoice.m_voiceengine is null");
                return false;
            }

            int ret = m_voiceengine.OpenSpeaker();
            Debug.Log("GCloudVoiceSDK OpenSpeaker , ret := " + ret.ToString());
            if (ret != (int)GCloudVoiceErr.GCLOUD_VOICE_SUCC)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        ///     关闭扬声器
        /// </summary>
        /// <returns>执行成功返回true, 执行失败返回false</returns>
        public static bool CloseSpeaker()
        {
            if (m_voiceengine == null)
            {
                Debug.Log("RealTimeVoice.m_voiceengine is null");
                return false;
            }

            int ret = m_voiceengine.CloseSpeaker();
            Debug.Log("GCloudVoiceSDK CloseSpeaker , ret := " + ret.ToString());
            if (ret != (int)GCloudVoiceErr.GCLOUD_VOICE_SUCC)
            {
                return false;
            }

            return true;
        }


        /// <summary>
        ///     设置扬声器音量
        /// </summary>
        /// <param name="volume">音量</param>
        /// <returns>执行成功返回true, 执行失败返回false</returns>
        public static bool SetSpeakerVolume(int volume)
        {
            if (m_voiceengine == null)
            {
                Debug.Log("RealTimeVoice.m_voiceengine is null");
                return false;
            }

            int ret = m_voiceengine.SetSpeakerVolume(volume);
            Debug.Log(
                "GCloudVoiceSDK SetSpeakerVolume , volume := " + volume.ToString() + " , ret := " + ret.ToString());
            if (ret != (int)GCloudVoiceErr.GCLOUD_VOICE_SUCC)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        ///     获取扬声器音量
        /// </summary>
        /// <returns>返回扬声器音量</returns>
        public static int GetSpeakerVolume()
        {
            if (m_voiceengine == null)
            {
                Debug.Log("RealTimeVoice.m_voiceengine is null");
                return (int)GCloudVoiceErr.GCLOUD_VOICE_NEED_INIT; //4015    没有初始化
            }

            int ret = m_voiceengine.GetSpeakerLevel();
            Debug.Log("GCloudVoiceSDK GetSpeakerVolume , ret := " + ret.ToString());
            return ret;
        }

        /// <summary>
        ///     使能/禁止成员发言
        /// </summary>
        /// <param name="memberID">成员ID</param>
        /// <param name="bEnable">使能/禁止</param>
        /// <returns>执行成功返回true, 执行失败返回false</returns>
        public static bool ForbidMemberVoice(int memberID, bool bEnable)
        {
            if (m_voiceengine == null)
            {
                Debug.Log("RealTimeVoice.m_voiceengine is null");
                return false;
            }

            int ret = m_voiceengine.ForbidMemberVoice(memberID, bEnable);
            Debug.Log("GCloudVoiceSDK ForbidMemberVoice , memberID := " + memberID.ToString() + " , bEnable := " +
                      bEnable.ToString() + " , ret := " + ret.ToString());
            if (ret != (int)GCloudVoiceErr.GCLOUD_VOICE_SUCC)
            {
                return false;
            }

            return true;
        }

        public static bool ChangeRole(VoiceRole newRole)
        {
            if (m_voiceengine == null)
            {
                Debug.Log("RealTimeVoice.m_voiceengine is null");
                return false;
            }

            GCloudVoiceRole role = newRole == VoiceRole.Anchor ? GCloudVoiceRole.ANCHOR : GCloudVoiceRole.AUDIENCE;
            int ret = m_voiceengine.ChangeRole(role);
            Debug.Log("GCloudVoiceSDK ChangeRole , role := " + role.ToString() + " , ret := " + ret.ToString());
            if (ret != (int)GCloudVoiceErr.GCLOUD_VOICE_SUCC)
            {
                return false;
            }

            return true;
        }

        public static bool PauseBGMPlay()
        {
            if (m_voiceengine == null)
            {
                Debug.Log("RealTimeVoice.m_voiceengine is null");
                return false;
            }

            int ret = m_voiceengine.PauseBGMPlay();
            Debug.Log("GCloudVoiceSDK PauseBGMPlay , ret := " + ret.ToString());
            if (ret != (int)GCloudVoiceErr.GCLOUD_VOICE_SUCC)
            {
                return false;
            }

            return true;
        }

        public static bool ResumeBGMPlay()
        {
            if (m_voiceengine == null)
            {
                Debug.Log("RealTimeVoice.m_voiceengine is null");
                return false;
            }

            int ret = m_voiceengine.ResumeBGMPlay();
            Debug.Log("GCloudVoiceSDK ResumeBGMPlay , ret := " + ret.ToString());
            if (ret != (int)GCloudVoiceErr.GCLOUD_VOICE_SUCC)
            {
                return false;
            }

            return true;
        }
    }
}